define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'spider.task/index' + location.search,
                    add_url: 'spider.task/add',
                    // edit_url: 'spider.task/edit',
                    del_url: 'spider.task/del',
                    multi_url: 'spider.task/multi',
                    import_url: 'spider.task/import',
                    detail_url: 'spider.spider/index',
                    table: 'task',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        // {field: 'user_id', title: __('用户ID')},
                        {field: 'platform.name', title: __('Platform'), operate: 'LIKE', formatter: Table.api.formatter.label},
                        {field: 'admin.nickname', title: __('User_id'), operate: 'LIKE'},
                        {field: 'get_url', title: __('Get_url'), operate: 'LIKE', formatter: Table.api.formatter.url},
                        {field: 'shop_count', title: __('商品数量'), operate:'between', formatter: Table.api.formatter.label},
                        {field: 'use_time', title: __('Use_time'), operate:'between', formatter: Table.api.formatter.label},
                        {field: 'status', title: __('Status'), searchList: {"1":__('Status 1'),"2":__('Status 2'),"3":__('Status 3'),"4":__('Status 4')}, formatter: Table.api.formatter.status},
                        // {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, buttons: [
                                {
                                    name: 'detail',
                                    text: __('查看任务结果'),
                                    title: __('查看任务结果'),
                                    classname: 'btn btn-xs btn-primary btn-dialog',
                                    icon: 'fa fa-list',
                                    extend: "data-area='[\"100%\",\"100%\"]'",
                                    url: function (row) {
                                        return $.fn.bootstrapTable.defaults.extend.detail_url + "?task_id=" + row.id;
                                    },
                                    // visible: function (row) {
                                    //     //返回true时按钮显示,返回false隐藏
                                    //     return true;
                                    // }
                                },
                            ], formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
